#!/bin/bash

# X服务器环境和日志检查脚本

echo "======= X服务器环境检查 ======="
echo "显示环境变量:"
echo "DISPLAY = $DISPLAY"
echo "XDG_SESSION_TYPE = $XDG_SESSION_TYPE"

echo -e "\n正在检查X服务器进程..."
ps aux | grep X | grep -v grep

echo -e "\n正在检查X服务器套接字..."
ls -la /tmp/.X11-unix/

echo -e "\n正在检查最近的X服务器日志错误..."
if [ -f /var/log/Xorg.0.log ]; then
    grep -i error /var/log/Xorg.0.log | tail -20
    echo -e "\n最近的警告:"
    grep -i warning /var/log/Xorg.0.log | tail -10
else
    echo "找不到Xorg日志文件。尝试使用journalctl"
    journalctl -b | grep Xorg | grep -i -E 'error|warning' | tail -20
fi

echo -e "\n======= X应用程序调试工具 ======="
echo "可用的X11调试工具:"
which xwininfo > /dev/null && echo "xwininfo - 已安装" || echo "xwininfo - 未安装"
which xprop > /dev/null && echo "xprop - 已安装" || echo "xprop - 未安装"
which xev > /dev/null && echo "xev - 已安装" || echo "xev - 未安装"
which strace > /dev/null && echo "strace - 已安装" || echo "strace - 未安装"

echo -e "\n======= X连接测试 ======="
echo "尝试运行基本X命令测试连接:"
which xdpyinfo > /dev/null && (xdpyinfo > /dev/null && echo "X服务器连接正常" || echo "X服务器连接失败") || echo "xdpyinfo 未安装"

echo -e "\n要分析更详细的X客户端与服务器通信，请运行:"
echo "LD_PRELOAD=/usr/lib/libxtrace.so ./simple_x11_client"
echo "(注意：可能需要安装libxtrace：apt install xtrace 或 yum install xtrace)"
